package com.gaoyi.crm.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.apiresult.PagedList;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.callback.OnSyncListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.db.bean.ResultBean;
import com.gaoyi.crm.db.entity.PmnWithList;
import com.gaoyi.crm.db.query.MtcSyncedQuery;
import com.gaoyi.crm.fragment.mtc.MtcNoSyncListFragment;
import com.gaoyi.crm.repository.PmnRepository;
import com.gaoyi.crm.util.StringUtil;
import com.gaoyi.crm.util.SyncUploadUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.base.BaseListViewModel;

import java.util.List;

public class MtcNoSyncListViewModel extends BaseListViewModel<PmnWithList> {

    MutableLiveData<Integer> mTotal;
    MutableLiveData<ResultBean> mResult;
    final PmnRepository mRepository;

    public MutableLiveData<ResultBean> getResult() {
        if (mResult == null) {
            mResult = new MutableLiveData<>();
        }
        return mResult;
    }

    public void setResult(ResultBean result) {
        mResult.setValue(result);
    }

    public MutableLiveData<Integer> getTotal() {
        if (mTotal == null) {
            mTotal = new MutableLiveData<>();
        }
        return mTotal;
    }

    public MtcNoSyncListViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PmnRepository();
    }

    public void getNoSyncPmn(MtcSyncedQuery query) {
        mRepository.getNoSyncPmn(query, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<PagedList<PmnWithList>> apiResultData = (ApiResultData<PagedList<PmnWithList>>) apiResult;
                mResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                PagedList<PmnWithList> data = apiResultData.getData();
                if (data != null) {
                    mTotal.setValue(data.getTotalCount());
                    List<PmnWithList> pmnWithLists = data.getList();
                    if (pmnWithLists != null && !pmnWithLists.isEmpty()) {
                        addListValue(pmnWithLists);
                    }
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        });
    }

    public void syncPmn(List<PmnWithList> pmns) {
        if (getView() != null) {
            getView().showLoading();
        }
        SyncUploadUtil.getInstance().syncPmn(pmns, new OnSyncListener() {
            @Override
            public void onSuccess() {
                if (getView() != null) {
                    getView().hideLoading();
                }
                setSyncResult(ApiStatus.SUCCESS, StringUtil.getString(R.string.sync_successfully), "");
            }

            @Override
            public void onError(String data) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                setSyncResult(ApiStatus.FAIL, StringUtil.getString(R.string.sync_failed_duplicate), StringUtil.getString(R.string.failed_pro_id,data));
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                setSyncResult(ApiStatus.FAIL, StringUtil.getString(R.string.sync_failed_network), null);
            }
        });
    }


    /**
     * 同步返回
     *
     * @param status 同步结果
     * @param remark 备注
     * @param tip    失败原因
     */
    private void setSyncResult(int status, String remark, String tip) {
        ResultBean bean = new ResultBean(status, remark, tip, MtcNoSyncListFragment.class.getSimpleName());
        mResult.setValue(bean);
    }
}
